﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DCSLibrary;
using System.Data;
using Utils.DatabaseUtils;
using Utils.Entity;
using Utils;
using Utils.DC.Web.UI;
using System.Collections;

public partial class Admin_Form_Pool_Add : System.Web.UI.Page
{
    FunctionPageUI objCategoryPage = new FunctionPageUI();
    PoolTopicUI objPool = new PoolTopicUI();
    DataSet dsCategoryPage;
    int poolCate = 28;

    protected void Page_Load(object sender, EventArgs e)
    {
        //dsCategoryPage = objCategoryPage.GetByID(28);
        if (!IsPostBack)
        {
            //BindCategoryPage();
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Trim() != "")
            {
                try
                {
                    LoadEditData(Convert.ToInt32(Request.QueryString["id"]));
                }
                catch (FormatException fe)
                {
                }
            }
        }
    }

    private void BindCategoryPage()
    {
        try
        {
            cboCategoryPage.DataSource = dsCategoryPage.Tables[0];
            cboCategoryPage.DataBind();
        }
        catch (Exception e)
        {

        }
    }

    protected void AddAnswer(object sender, EventArgs e)
    {
        string answer = txtAnswer.Text.Trim();
        if (answer != "")
        {
            lstTopicAnswer.Items.Add(answer);
        }
    }

    protected void RemoveAnswer(object sender, EventArgs e)
    {
        lstTopicAnswer.Items.Remove(lstTopicAnswer.SelectedValue);
    }

    private ArrayList GetAnswerList()
    {
        ArrayList al = new ArrayList();
        foreach (ListItem item in lstTopicAnswer.Items)
        {
            al.Add(item.Value);
        }

        return al;
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        UserEntity currentUser = (UserEntity)Session[PageUtility.SESSION_USER_NAME];
        if (txtPoolTopicID.Value.Trim() == "")
        {
            try
            {

                int result = objPool.Add(poolCate, txtTopicContent.Text.Trim(), DateTime.Now, Request.UserHostAddress, currentUser.UserID, chkIsActive.Checked, chkIsTypical.Checked, GetAnswerList());

                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                //lblMessage.Text = MessageBox.MESSAGE_DUPLICATE_DATA;
                lblMessage.Text = ex.ToString();
            }
        }
        else
        {
            try
            {

                int result = objPool.Update(Convert.ToInt32(txtPoolTopicID.Value), poolCate, txtTopicContent.Text.Trim(), DateTime.Now, Request.UserHostAddress, currentUser.UserID, chkIsActive.Checked, chkIsTypical.Checked, GetAnswerList());
                if (result > 0)
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_SUCCESSFULLY;
                    ReInsertData();
                }
                else
                {
                    //Response.Write(messageBox.Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
                    lblMessage.Text = MessageBox.MESSAGE_UPDATE_NOT_SUCCESSFULLY;
                }
            }
            catch (Exception ex)
            {
                //Response.Write(messageBox.Alert(MessageBox.MESSAGE_DUPLICATE_DATA));
                lblMessage.Text = MessageBox.MESSAGE_CONFILCT_DATA;
            }
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Response.Redirect("/Admin/quan-tri-noi-dung/quan-tri-pool.aspx");
    }

    private void ReInsertData()
    {
        //txtPoolTopicID.Value = "";
        txtTopicContent.Text = "";
        lstTopicAnswer.Items.Clear();
        txtAnswer.Text = "";
        chkIsTypical.Checked = false;
        chkIsActive.Checked = false;
        General.SetSelectedItemInComboBox(null, cboCategoryPage);
    }

    private void LoadEditData(int PoolTopicID)
    {
        DataSet ds = objPool.GetByID(PoolTopicID);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            DataRow row = ds.Tables[0].Rows[0];
            txtPoolTopicID.Value = row["PoolTopicID"].ToString();
            txtTopicContent.Text = row["TopicContent"].ToString();
            chkIsTypical.Checked = Convert.ToBoolean(row["IsTypical"]);
            chkIsActive.Checked = Convert.ToBoolean(row["IsActive"]);
            LoadAnswers(ds.Tables[1]);
            //General.SetSelectedItemInComboBox(row["CategoryPageID"].ToString(), cboCategoryPage);
        }
    }

    private void LoadAnswers(DataTable dt)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            foreach (DataRow row in dt.Rows)
            {
                lstTopicAnswer.Items.Add(row["PoolAnswerContent"].ToString());
            }
        }
    }
}
